library(SDMTools)
climgen.dir='/home/22/jc148322/CLIMGEN/'

#climgen.dir='E:/Jeremy Work Directory/CLIMGEN/'

tt = readLines(paste(climgen.dir, 'pre_patCMIP3cccma_cgcm31___A2A1B_____dtUSERSEL__20342063ann_monthly_reglandboxes_______.climgen', sep=''))
data=tt[26:134865]
#test=tt[26:35]

latlon=seq(1,134840,2)

yr.precip=seq(2,134840,2)

subset1 = tt[latlon]

subset2=tt[yr.precip]

#latlon=seq(1,10,2)
#yr.precip=seq(2,10,2)
#subset1 = test[latlon]
#subset2=test[yr.precip]


loc=NULL
for (ii in 1:length(subset1)){cat(ii,'\n')
jj=subset1[ii]
bb=as.numeric(unlist(strsplit(jj,' ')))
bb=bb[which(is.finite(bb))]
if (length(loc) == 0) { #if there are no values , populate it
          loc=cbind(bb[6],bb[7])
        } else { #if already has values, add the new values to it
          loc = rbind(loc,cbind(bb[6],bb[7]))
		 } 

}

yr=NULL
for (ii in 1:length(subset2)){cat(ii,'\n')
jj=subset2[ii]
bb=as.numeric(unlist(strsplit(jj,' ')))
bb=bb[which(is.finite(bb))]
if (length(yr) == 0) { #if there are no values , populate it
          yr=cbind(bb[1],bb[2])
        } else { #if already has values, add the new values to it
          yr = rbind(yr,cbind(bb[1],bb[2]))
		 } 

}

df=cbind(loc,yr)

pr=NULL

for (ii in 1:length(subset2)){cat(ii,'\n')
jj=subset2[ii]
bb=as.numeric(unlist(strsplit(jj,' ')))
bb=bb[which(is.finite(bb))]
bb=bb[3:14]

if (length(pr) == 0) { #if there are no values , populate it
          pr=bb
        } else { #if already has values, add the new values to it
          pr = rbind(pr,bb)
		 } 
}
df=cbind(df,pr)
copy.df=df
copy.df=data.frame(copy.df)

month=seq(1,12,1)
colnames(copy.df)=c('lat','long','start.yr','end.yr','1','2','3','4','5','6','7','8','9','10','11','12')
colnames(copy.df)=c('lat','long','start.yr','end.yr',month)
rownames(copy.df)=NULL


